import java.util.Random;
import java.util.Scanner;

public class Game {
    private Player humanPlayer;
    private Player PCPlayer;
    private int systemNumber;

    public Game() //default constructor
    {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个玩家名字");
        String humName = scanner.next();
        //定义两个人分别猜了一个数字
        if (humName.length() > 8) {
            System.out.println("玩家名字长度超过八个字符了,重新定义");
            return;
        }
        humanPlayer = new Player(humName, 0, 0);
        PCPlayer = new Player("电脑", 0, 0);
    }

    //public void start() {
    //    Game game = new Game();
    //    game.Start(); //输出欢迎信息
    //    humanEnterGameName();
    //    int round = 1;
    //    while (round < 5) // Need to play 4 rounds
    //    {
    //        int low = 1; //最小为1
    //        int high = 100; //最大100
    //        int randomNum = generateRandomNum(); //产生随机数
    //        boolean flag = true;
    //        int attempt = 0; //准备猜
    //
    //        boolean humanAbandon = false;
    //        boolean PCAbandon = false;
    //        while (attempt < 7) //每轮每个参与者可以猜三次
    //        {
    //            attempt++;
    //            int humanGuessNum = humanGuessNum(); //Human先猜
    //            if (humanGuessNum == randomNum) //Person guess the right numebr
    //            {
    //                break;  //correct,Current round over
    //            }
    //            if (humanGuessNum == 999) //human输入999代表放弃本轮
    //            {
    //                humanAbandon = true;
    //                break; //human放弃，结束本轮循环
    //            }
    //
    //            while (humanGuessNum < 1 || humanGuessNum > 100) //在数值范围内
    //            {
    //                humanGuessNum = humanGuessNum();
    //                System.out.println("Warnning! You are out of the range, please enter again");//出现warnning信息，提示重新输入
    //            }
    //
    //            if (humanGuessNum >= 1 || humanGuessNum <= 100 && (humanGuessNum < low) || (humanGuessNum > high)) //缩小范围
    //            {
    //                System.out.println("Warnning! You are not in the positive range! One attempt wasted!"); //不允许再次输入
    //            } else if (checkHigherOrLower(humanGuessNum, randomNum, 1)) {
    //                high = humanGuessNum; //若猜大了，则最高值改为当前guessnumber
    //            } else {
    //                low = humanGuessNum;
    //            }
    //
    //            attempt++;
    //            int PCGuessNum = PCGuessNum();
    //            if (PCGuessNum >= 1 || PCGuessNum <= 100) {
    //                if (PCGuessNum == randomNum) {
    //                    break;
    //                }
    //                if (PCGuessNum == 999) //若对手在本轮尝试中放弃则我方取得本轮尝试所对应的分数，无需进入下一轮
    //                {
    //                    PCAbandon = true;
    //                    break;
    //                }
    //            }
    //
    //            if (checkHigherOrLower(PCGuessNum, randomNum, 1)) {
    //                high = PCGuessNum;
    //            } else {
    //                low = PCGuessNum;
    //            }
    //        }
    //        humanPlayer.setScore(getAttemptScore(attempt)); //调用player中的score方法将attempt传入
    //        setPlayerScore(attempt, humanAbandon, PCAbandon, randomNum, humanPlayer.getGuess(), PCPlayer.getGuess());
    //    }
    //}

    public void Start() {
        System.out.println("Welcome to the NumberGuess Game!");
        System.out.println(" This game have 4 round, each round you will be given 3 chances to guess the BingoNum. Good luck!");
    }


    public void startGames() {
        System.out.println("新一轮开始了准备好了吗");
        Random random = new Random();
        systemNumber = random.nextInt(100) + 1;
        //系统生成一个随机数
        systemNumber = systemNumber == 100 ? 99 : systemNumber;
        /**
         *放弃的回合数
         */
        int giveUp = random.nextInt(20) + 1;
        //最大值和最小值
        int min = 1;
        int max = 100;
        /**
         *比赛状态0 表示有人猜对游戏结束   1标识三回合没有人猜对
         */
        int gameStatus = 1;

        /**
         *一轮三回合 三次循环
         */
        for (int i = 0; i < 3; i++) {
            System.out.println("---------------------------");
            System.out.println("人"+humanPlayer.getGuessCount());
            System.out.println("电脑"+PCPlayer.getGuessCount());
            System.out.println("---------------------------");

            NumberGenerator personNumber = humanPlayer.guessNumber(min, max);
            //给玩家设置最后的猜测次数
            humanPlayer.setEndNumber(personNumber.getNumber());
            /**
             *如果玩家没有收到惩罚,更新最大或者最小值
             */
            if (!personNumber.isFlag()) {
                if (personNumber.getNumber() > systemNumber) {
                    max = personNumber.getNumber();
                    System.out.println("你猜的偏大了");
                } else if(personNumber.getNumber()<systemNumber) {
                    min = personNumber.getNumber();
                    System.out.println("你猜的偏小了");
                }
            }
            //如果尝试次数等于放弃指标  或者 玩家输入了999 放弃比赛  电脑加分

            if (personNumber.getNumber() == 999) {
                System.out.println("玩家放弃本轮比赛");
                gameStatus = 0;
                System.out.println("总猜测次数"+(humanPlayer.getGuessCount() + PCPlayer.getGuessCount()));
                PCPlayer.addscore((humanPlayer.getGuessCount() + PCPlayer.getGuessCount()));
                break;
            }

//            if (isGiveUp(humanPlayer.getGuessCount(), PCPlayer.getGuessCount(), giveUp)) {
//                System.out.println("随机放弃指标 本回合结束 随机数为" + giveUp);
//                PCPlayer.addscore((humanPlayer.getGuessCount() + PCPlayer.getGuessCount()));
//                break;
//            }


            //如果玩家猜对 玩家加分
            if (personNumber.getNumber() == systemNumber) {
                gameStatus = 0;
                humanPlayer.addscore(humanPlayer.getGuessCount() + PCPlayer.getGuessCount());
                System.out.println("本轮结束");
                break;
            }
            //电脑开始猜数字
            NumberGenerator pc = new NumberGenerator();
            int pcNumber = pc.generateRandom(min, max);
            //电脑增加猜测次数
            PCPlayer.addGuessCount();
            PCPlayer.setEndNumber(pcNumber);
            System.out.println("电脑猜了" + pcNumber);
            //更新最大值和最小值
            if (pcNumber > systemNumber) {
                System.out.println("电脑猜的偏大了");
                max = pcNumber;
            } else if(pcNumber<systemNumber){
                System.out.println("电脑猜的偏小了");
                min = pcNumber;
            }
            //如果电脑猜测完后 等于放弃指标 玩家加分
            if (isGiveUp(humanPlayer.getGuessCount(), PCPlayer.getGuessCount(), giveUp)) {
                System.out.println("随机放弃指标 本回合结束 随机数为" + giveUp);
                humanPlayer.addscore(humanPlayer.getGuessCount() + PCPlayer.getGuessCount());
                break;
            }
            //如果电脑猜测正确  电脑加分
            if (pcNumber == systemNumber) {
                gameStatus = 0;
                PCPlayer.addscore(humanPlayer.getGuessCount() + PCPlayer.getGuessCount());
                System.out.println("本轮结束");
                break;
            }
        }
        //如果三个回合结束  双反都没有猜对 看谁距离 系统的猜测分最近 谁就加分
        if (gameStatus == 1) {
            System.out.println("本轮结束 随机数是" + systemNumber);
            System.out.println("你最后猜测的是" + humanPlayer.getEndNumber());
            System.out.println("电脑最后猜测的是" + PCPlayer.getEndNumber());
            int h = Math.abs(humanPlayer.getEndNumber() - systemNumber);
            int p = Math.abs(PCPlayer.getEndNumber() - systemNumber);
            if (h < p) {
                humanPlayer.addscore(humanPlayer.getGuessCount() + PCPlayer.getGuessCount());
            } else {
                PCPlayer.addscore(humanPlayer.getGuessCount() + PCPlayer.getGuessCount());
            }

        }

        humanPlayer.setGuessCount(0);
        PCPlayer.setGuessCount(0);

    }

    private boolean isGiveUp(int person, int pc, int giveUp) {
        if (person + pc == giveUp) {
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        return "Game{" +
                "humanPlayer=" + humanPlayer.toString() +
                ", PCPlayer=" + PCPlayer.toString() +
                '}';
    }


    public static void main(String[] args) {
        Game game = new Game();
        game.Start();
        for (int i = 0; i < 4; i++) {
            game.startGames();
        }
        System.out.println("游戏最终情况如下" + game);
    }
}
